<?php
require_once __DIR__ . '/sqlConnect.php';
require_once __DIR__ . '/sqlConnect.php';

class SQL
{
    public $conn;
    public $tableName = null;

    function __construct($tName = '', $theConn = null)
    {
        global $conn;
        if (is_null($theConn)) {
            $theConn = $conn;
        }
        $this->conn = $theConn;


        $this->tableName = $tName;
    }
    function columnNamesArray2String($columnNamesArray)
    {
        return implode(',', $columnNamesArray);
    }
    function clauseArray2String($clauseArray)
    {
        return implode(' AND ', $clauseArray);
    }
    public function delete($clauseArray, &$error = null)
    {

        $clause = $this->clauseArray2String($clauseArray);

        $whereStr = $clause === '' ? '' : " WHERE {$clause}";
        $sqlStr = "DELETE FROM {$this->tableName}{$whereStr}";
        $result = $this->conn->query($sqlStr);

        $error = $this->conn->error ?? null;
        return !!$result;
    }
    public function select($columnNamesArray = array('*'), $clauseArray, &$error = null, $extraSqlStr = '')
    {

        $columnNames = $this->columnNamesArray2String($columnNamesArray);
        $clause = $this->clauseArray2String($clauseArray);

        $whereStr = $clause === '' ? '' : " WHERE {$clause}";

        $sqlStr = "SELECT {$columnNames} FROM {$this->tableName}{$whereStr}{$extraSqlStr}";

        $result = $this->conn->query($sqlStr);

        $error = $this->conn->error ?? null;


        $resultArray = array();

        if ($result && $result->num_rows > 0) {
            // 输出数据
            while ($row = $result->fetch_assoc()) {
                $resultArray[] = $row;
            }
            return $resultArray;
        }
        return false;
    }
    public function customSql($sqlStr, &$error = null)
    {

        $result = $this->conn->query($sqlStr);

        $error = $this->conn->error ?? null;


        $resultArray = array();

        if ($result) {
            if ($result->num_rows > 0) {

                // 输出数据
                while ($row = $result->fetch_assoc()) {
                    $resultArray[] = $row;
                }
            }
            return $resultArray;
        }
        return false;
    }
    public function update($setArray, $clauseArray, &$error = null)
    {

        $setStringArray = array();
        foreach ($setArray as $key => $value) {
            $setStringArray[] =  "{$key} = {$value}";
        }
        $setString = implode(', ', $setStringArray);

        $clause = $this->clauseArray2String($clauseArray);
        $sqlStr = "UPDATE {$this->tableName} SET {$setString} WHERE {$clause}";


        $result = $this->conn->query($sqlStr);
        $error = $this->conn->error ?? null;

        return !!$result;
    }
    public function insertAndUpdate($setArray, $clauseArray, &$error = null)
    {
        $result = $this->select(array('*'), $clauseArray, $error);
        if (!$result || count($result) <= 0) {
            if ($this->insert($setArray, $error) === false) {
                return false;
            }
        }
        return $this->update($setArray, $clauseArray, $error);
    }
    public function insert($insertArray, &$error = null)
    {

        $insertColumnNameArray = array();
        $insertValueArray = array();

        foreach ($insertArray as $key => $value) {
            $insertColumnNameArray[] = $key;
            // var_dump($value);
            $insertValueArray[] = $value;
        }

        $columnNameString = implode(',', $insertColumnNameArray);
        $ValueString = implode(',', $insertValueArray);

        $sqlStr = "INSERT INTO {$this->tableName} ({$columnNameString}) VALUES ({$ValueString})";
        // echo "insert sql {$sql}";
        $result = $this->conn->query($sqlStr);
        $error = $this->conn->error ?? null;

        return $result;
    }
    /**
     * 慎用!!!!
     */
    public function replace($insertArray, &$error = null)
    {

        $insertColumnNameArray = array();
        $insertValueArray = array();

        foreach ($insertArray as $key => $value) {
            $insertColumnNameArray[] = $key;
            // var_dump($value);
            $insertValueArray[] = $value;
        }

        $columnNameString = implode(',', $insertColumnNameArray);
        $ValueString = implode(',', $insertValueArray);

        $sqlStr = "REPLACE INTO {$this->tableName} ({$columnNameString}) VALUES ({$ValueString})";
        // echo "insert sql {$sql}";
        $result = $this->conn->query($sqlStr);
        $error = $this->conn->error ?? null;

        return $result;
    }
}
